package com.example.spring_leran1.controller;

import com.example.spring_leran1.entity.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.example.spring_leran1.utils.Result;
import  com.example.spring_leran1.utils.JwtUtils;

import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @ApiOperation("获取用户")
    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable() int id) {
        System.out.println(id);
        return "根据id获取用户信息";
    }
    @PostMapping("/user")
    public String save(User user){return "添加用户";}
    @PutMapping("/user")
    public String update( User user){return "更新用户";}
    @DeleteMapping("/user/{id}")
    public String deleteById(@PathVariable() int id){
        System.out.println(id);
        return "根据ID删除用户";
    }

    //进行token生成并获取
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String token=JwtUtils.generateTokren(user.getUsername());
        return Result.ok().data(Map.of("token", token));
    }

    @GetMapping("/info")
    public Result info(String token){
        String username=JwtUtils.getClaimsByToken(token).getSubject();
        String url=" ";
        return Result.ok().data(Map.of("username", username,"avatar", url));
    }

    @PostMapping("/logout")
    public Result logout(){return Result.ok();}
}
